<template>
    <div class="" style="background-color: #333742;">
        <div v-if="loading"
             class="loader-wrapper">
            <pulse-loader></pulse-loader>
        </div>

        <div v-if="userInfo" class="modal" id="signin-modal2">
            <div class="modal-background"></div>
            <div class="modal-card">
              <header class="modal-card-head">
                <p class="modal-card-title">YDapp签到</p>
                <button class="delete" aria-label="close" @click="hideSignIn()"></button>
              </header>
              <section class="modal-card-body">
                <div class="has-text-black has-text-weight-bold has-text-left">签到题: {{userInfo.sign_in_config.content}}? </div>

                <div class="control">
                  <div v-if="userInfo.sign_in_config.config.choice[0]">
                    <label class="radio">
                      <input type="radio" name="answer" v-model="signAnswer" value="1">
                      {{userInfo.sign_in_config.config.choice[0]}}
                    </label>
                  </div>
                  <div v-if="userInfo.sign_in_config.config.choice[1]">
                    <label class="radio">
                      <input type="radio" name="answer" v-model="signAnswer" value="2">
                      {{userInfo.sign_in_config.config.choice[1]}}
                    </label>
                  </div>
                  <div v-if="userInfo.sign_in_config.config.choice[2]">
                    <label class="radio">
                      <input type="radio" name="answer" v-model="signAnswer" value="3">
                      {{userInfo.sign_in_config.config.choice[2]}}
                    </label>
                  </div>
                  <div v-if="userInfo.sign_in_config.config.choice[3]">
                    <label class="radio">
                      <input type="radio" name="answer" v-model="signAnswer" value="4">
                      {{userInfo.sign_in_config.config.choice[3]}}
                    </label>
                  </div>
                  <div class="has-text-info" style="">* 连续签到第{{userInfo.c_sign_in_days+1}}天, 回答成功获得{{2*(userInfo.c_sign_in_days+1)}}YDAPP, 失败获得{{userInfo.c_sign_in_days+1}}YDAPP</div>
                </div>
                <!-- Content ... -->
              </section>
              <footer class="modal-card-foot">
                <button class="button is-success" @click="signIn()">签到</button>
                <button class="button" @click="hideSignIn()">取消</button>
              </footer>
            </div>

            <button class="modal-close is-large" aria-label="close" @click="hideSignIn()"></button>
          </div>


        <div class="container main-container has-text-white">

          <div v-if="userInfo && userInfo.is_approve && !userInfo.has_signed">
            <div class="button is-primary is-outlined" @click="showSignIn()">
              签到<span class="has-text-warning">&nbsp;New!!</span>
            </div>
          </div>
          <div v-else-if="userInfo && userInfo.is_approve && userInfo.has_signed" class="has-text-grey"> 已签到 </div>
          <div v-else="" class="has-text-danger" style=""> 认证用户签到得YDapp, 认证请扫页面内二维码  </div>

          <div class="has-text-centered has-text-danger is-size-1"> YDAPP 圣诞嘉年华 </div> 

          <div class="has-text-centered" style="margin-top: 3rem;">
            <div class="button is-danger is-outlined is-size-1" style="border-radius: 1rem;" @click="lottery()"> 抽奖 </div>
            <div v-if="playerInfo" class="" style="margin-top: 1rem;"> 剩余 {{playerInfo.lottery_times}} 次抽奖机会 </div>
            <div v-else class="" style="margin-top: 1rem;"> 剩余 0 次抽奖机会 </div>
          </div>
          <div class="columns" style="padding-top: 3rem;">
            <div class="column is-2"></div>

            <div class="column is-8">

            <div class="columns is-mobile is-multiline">
            <div class="column is-2-desktop has-text-centered is-half-mobile">
              <figure class="image is-128x128" style="margin: auto;">
                <img src="/static/assets/icon2.png">
              </figure>
              <div v-if="playerInfo.p1 >= 1" class="has-text-centered has-text-warning"> 共有 {{playerInfo.p1}}/1 个 </div>
              <div v-else class="has-text-centered has-text-danger"> 共有 {{playerInfo.p1}}/1 个 </div>
            </div>
            <div class="column is-2-desktop has-text-centered is-half-mobile">
              <figure class="image is-128x128" style="margin: auto;">
                <img src="/static/assets/y.png">
              </figure>
              <div v-if="playerInfo.p2 >= 1" class="has-text-centered has-text-warning"> 共有 {{playerInfo.p2}}/1 个 </div>
              <div v-else class="has-text-centered has-text-danger"> 共有 {{playerInfo.p2}}/1 个 </div>
            </div>
            <div class="column is-2-desktop has-text-centered is-half-mobile">
              <figure class="image is-128x128" style="margin: auto;">
                <img src="/static/assets/d.png">
              </figure>
              <div v-if="playerInfo.p3 >= 1" class="has-text-centered has-text-warning"> 共有 {{playerInfo.p3}}/1 个 </div>
              <div v-else class="has-text-centered has-text-danger"> 共有 {{playerInfo.p3}}/1 个 </div>
            </div>
            <div class="column is-2-desktop has-text-centered is-half-mobile">
              <figure class="image is-128x128" style="margin: auto;">
                <img src="/static/assets/a.png">
              </figure>
              <div v-if="playerInfo.p4 >= 1" class="has-text-centered has-text-warning"> 共有 {{playerInfo.p4}}/1 个 </div>
              <div v-else class="has-text-centered has-text-danger"> 共有 {{playerInfo.p4}}/1 个 </div>
            </div>
            <div class="column is-2-desktop has-text-centered is-half-mobile">
              <figure class="image is-128x128" style="margin: auto;">
                <img src="/static/assets/p.png">
              </figure>
              <div v-if="playerInfo.p5 >= 2" class="has-text-centered has-text-warning"> 共有 {{playerInfo.p5}}/2 个 </div>
              <div v-else class="has-text-centered has-text-danger"> 共有 {{playerInfo.p5}}/1 个 </div>
            </div>
            <div class="column is-2-desktop has-text-centered is-half-mobile">
              <figure class="image is-128x128" style="margin: auto;">
                <img src="/static/assets/p.png">
              </figure>
              <div v-if="playerInfo.p5 >= 2" class="has-text-centered has-text-warning"> 共有 {{playerInfo.p5}}/2 个 </div>
              <div v-else class="has-text-centered has-text-danger"> 共有 {{playerInfo.p5}}/1 个 </div>
            </div>
          </div>

          <div class="has-text-centered">
            <div v-if="canWithdraw" class="button is-success is-outlined" style="margin-top: 2rem;" @click="claim()"> 兑换1 EOS </div>
            <div v-else class="button is-success is-outlined" style="margin-top: 2rem;" disabled> 兑换1 EOS </div>
          </div>

          <div class="columns has-text-centered" style="margin-top: 2rem;">
            <div class="column is-3"> </div>
            <div class="column is-2"> 转赠他人 </div>
            <div class="column is-4">
              <input class="input" type="text" id="gift_address" placeholder="转赠人地址" v-model="gift_address">
            </div>
          </div>

          <div class="columns has-text-centered is-mobile is-multiline">
            <div class="column is-2-desktop is-half-mobile">
              <div v-if="playerInfo.p1 > 0" class="button is-success is-outlined" @click="transfercard(0)"> 赠麋鹿 </div>
              <div v-else class="button is-success is-outlined" disabled> 赠麋鹿 </div>
            </div>
            <div class="column is-2-desktop is-half-mobile">
              <div v-if="playerInfo.p2 > 0" class="button is-success is-outlined" @click="transfercard(1)"> 赠Y </div>
              <div v-else class="button is-success is-outlined" disabled> 赠Y </div>
            </div>
            <div class="column is-2-desktop is-half-mobile">
              <div v-if="playerInfo.p3 > 0" class="button is-success is-outlined" @click="transfercard(2)"> 赠D </div>
              <div v-else class="button is-success is-outlined" disabled> 赠D </div>
            </div>
            <div class="column is-2-desktop is-half-mobile">
              <div v-if="playerInfo.p4 > 0" class="button is-success is-outlined" @click="transfercard(3)"> 赠A </div>
              <div v-else class="button is-success is-outlined" disabled> 赠A </div>
            </div>
            <div class="column is-2-desktop is-half-mobile">
              <div v-if="playerInfo.p5 > 0" class="button is-success is-outlined" @click="transfercard(4)"> 赠P </div>
              <div v-else class="button is-success is-outlined" disabled> 赠P </div>
            </div>
          </div>

          </div>

          </div>
        </div>

        <audio id="myAudio" autoplay="autoplay" loop="loop" src="https://static.togetthere.cn/xmas2.mp3">
        </audio>

    </div>
</template>

<script>
    import PulseLoader from 'vue-spinner/src/PulseLoader';
    import API, { eos, currentEOSAccount } from '@/util/api';
    import RightAd from '@/components/RightAd';
    import { mapActions, mapState } from 'vuex';

    export default {
        name: 'index',
        components: {
            PulseLoader,
            RightAd
        },

        data() {
            return {
                tokenPrice: [],
                news_list: [],
                signAnswer: 0,
                gift_address: '',
                loading: true,
            };
        },

        computed: {
          ...mapState(['scatterAccount', 'userInfo', 'playerInfo', 'playerInfoDict']),
          canWithdraw() {
            const result = ((this.playerInfo.p1 >= 1) && (this.playerInfo.p2 >= 1) && (this.playerInfo.p3 >= 1) && (this.playerInfo.p4 >= 1) && (this.playerInfo.p5 >= 2));
            return result;
          }
        },

        async created() {
          this.loading = false;
        },
        
        mounted: function () {
          var myAudio = $("#myAudio").get(0);
          myAudio.play();
        },

        methods: {
          ...mapActions(['loginScatterAsync', 'updatePlayerInfo']),
          timestampToStr(timestamp) {
              timestamp += 8*60*60;
              var result = new Date(parseInt(timestamp) * 1000).toISOString().replace('T', ' ').slice(10,16);
              return result;
          },
          async transfercard(cardId) {
            if (!this.gift_address) {
              alert("赠送地址不能为空");
              return;
            }
            if (!this.playerInfoDict[this.gift_address]) {
              alert("只能赠送给YDapp会员");
              return;
            }
            try {
              await API.transferCard({
                from: this.scatterAccount.name,
                to: this.gift_address,
                cardId: cardId
              })
              this.$store.dispatch('updatePlayerInfo', this.scatterAccount.name);
            } catch(error) {
              alert("赠送失败"+JSON.stringify(error));
              return;
            }
            alert("赠送成功");
          },
          async claim() {
            try {
              await API.claim({
                from: this.scatterAccount.name,
              })
              this.$store.dispatch('updatePlayerInfo', this.scatterAccount.name);
            } catch(error) {
              alert("领奖失败");
              return;
            }
            alert("领奖成功");
          },
          async lottery() {
            var myAudio = $("#myAudio").get(0);
            if (myAudio.paused) {
              myAudio.play();
            }
            if (this.playerInfo.lottery_times <= 0) {
              alert("没有抽奖次数");
              return;
            }
            // 抽奖
            try {
              await API.transferEOSAsync({
                from: this.scatterAccount.name,
                to: 'luckycat1111',
                amount: 500,
                memo: 'lottery',
              });
              this.$store.dispatch('updatePlayerInfo', this.scatterAccount.name);
            } catch (error) {
              alert(error);
            }
          },
          togglePlay() {
            var myAudio = $("#myAudio").get(0);
            if (myAudio.paused) {
              myAudio.play();
              $(".icon-music").show();
              $(".icon-play").hide();
            } else {
              myAudio.pause();
              $(".icon-music").hide();
              $(".icon-play").show();
            }
          },
          showSignIn() {
            $("#signin-modal2").addClass("is-active");
          },
          hideSignIn() {
            $("#signin-modal2").removeClass("is-active");
          },
          async signIn() {
            const result = await API.doSignIn(this.signAnswer);
            if (result.err_code == 0) {
              swal("签到成功", "又是元气满满的一天~", "success");
              this.hideSignIn();
              this.userInfo.has_signed = true;
            } else {
              swal("发送失败 ", result.err_msg, "error");
            }
          },
          isMobile() {
            var check = false;
            (function(a){if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
            return check;
          },
          async updateNews() {
            function sleep(ms) {
              return new Promise(resolve => setTimeout(resolve, ms));
            }
            for (var i=0;i<1000;i++) {
              await sleep(5000);
              this.news_list = await API.getNewsList();
            }
          }
        },
        watch: {},
    };
</script>
<style scoped>
    .loader-wrapper {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100vh;
    }
    .main-container {
      padding-top: 1rem;
      background-color: #333742;
    }
    .card {
        border:0.5px solid grey;
        border-radius: 2px;
        padding: 10px;
        background-color: #333742;
    }
    pre {
      padding: 0.1rem;
    }
</style>
